// pages/searchPassword/searchPassword.js
const app = getApp();
const API = require('../../common/constant');
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number: '',
    isSubmit: false,
    code: '',
    PWD: '',
    // confirmIdentity: false,
    inputCode: '',
    codeStatus: '获取验证码',
    confirmPWD: ''
  },
  /**
   * 获取手机号
   */
  getNumber: function (e) {
    this.setData({
      number: e.detail.value
    });
  },
  /**
   * 获取验证码
   */

  getPhoneCode: function () {
    let isGetCode = this.data.isGetCode;
    let that = this;
    let timer = '';
    let regMobile = /^0?1[3|4|5|8][0-9]\d{8}$/;//手机
    if (this.data.number.length == 0) {
      app.showUserInfo('请输入您的手机号码', 'none', 1500);
      return
    } else if (!regMobile.test(this.data.number)) {
      app.showUserInfo('请输入正确的手机号码格式', 'none', 1500);
      return
    } 
    if (isGetCode) {
      app.showUserInfo("一个手机60s内只能接收到一条验证码", "none", 2000);
      return
    }
    let mobile = this.data.number + "";
    this.setData({
      isGetCode: true
    })
    timer = setTimeout(function () {
      that.setData({
        isGetCode: false
      })
    }, 60000);
    let nowtime = 60;
    let codetime = setInterval(() => {
      nowtime--;
      this.setData({
        codeStatus: nowtime + 's'
      });
      if (nowtime == 0) {
        this.setData({
          codeStatus: '获取验证码'
        });
        clearInterval(codetime);
      }
    }, 1000);
    util.http({
      url: API.URL.USER.FIND_MOBILECODE,
      data: {
        mobile: mobile
      },
      success: (res) => {
        this.setData({
          code: res.data.bussData
        });

      },
      fail: (res) => {
        clearTimeout(timer)
        app.showUserInfo(res.msg, 'none', 1000)
        this.setData({
          isGetCode: false
        })
      }
    })
  },
  getCode: function (e) {
    let regMobile = /^0?1[3|4|5|8][0-9]\d{8}$/;//手机
    if (this.data.number.length == 0) {
      app.showUserInfo('请输入您的手机号码', 'none', 1500);
    } else if (!regMobile.test(this.data.number)) {
      app.showUserInfo('请输入正确的手机号码格式', 'none', 1500);
    } else {
      util.http({
        url: API.URL.USER.FIND_MOBILECODE,
        data: {
          mobile: this.data.number
        },
        success: res => {
          this.setData({
            code: res.data.bussData
          });
        },
        fail: err => {
          console.log(err);
        }
      });
    }
  },
  /**
   * 获取密码
   */
  getFirstPwd: function (e) {
    console.log(e);
    this.setData({
      PWD: e.detail.value
    });
  },
  getConfirmPwd: function (e) {
    console.log(e);
    this.setData({
      confirmPWD: e.detail.value
    })
  },
  /**
   * 获取输入的验证码
   */
  getInputCode: function (e) {
    this.setData({
      inputCode: e.detail.value
    })
  },
  /**
   * 提交信息
   */
  submitAll: function () {
    let that = this;
    if(this.data.isSubmit){
      return;
    }
    this.setData({
      isSubmit: true
    })
    let obj = this.data;
    if (obj.inputCode.length == 0 || obj.PWD.length == 0 || obj.confirmPWD.length == 0) {
      app.showUserInfo('请完善信息', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else if (obj.code.length == 0) {
      app.showUserInfo('请获取验证码', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else if (obj.inputCode.length != 6) {
      app.showUserInfo('请输入6位数的验证码', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else if (obj.code != obj.inputCode) {
      app.showUserInfo('验证码错误', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else if (obj.PWD.length < 6) {
      app.showUserInfo('密码长度不得小于6位', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else if (obj.PWD != obj.confirmPWD) {
      app.showUserInfo('密码不一致', 'none', 1500);
      this.setData({
        isSubmit: false
      })
      return;
    } else {
      console.log('信息完成');
      util.http({
        url: API.URL.USER.FIND_PWD,
        data: {
          mobile: obj.number,
          code: obj.inputCode,
          newPwd: obj.PWD,
          confirmPwd: obj.confirmPWD
        },
        success: res => {
          console.log(res);
          app.showUserInfo('重置密码成功', 'success', 1500);
          setTimeout(function () {
            that.setData({
              isSubmit: false
            })
            wx.navigateBack({})
          }, 1500);
        },
        fail: err => {
          that.setData({
            isSubmit: false
          })
          console.log(err);
          app.showUserInfo(err.msg, 'none', 1500)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})